Skip to content

Conversation

@ccisnedev
Copy link

@ccisnedev ccisnedev commented Oct 21, 2025

PR: fix(sqlgetdata): read large NVARCHAR/VARBINARY correctly via incremental SQLGetData fix #12

Summary

Fix corrupted strings returned by the Dart ODBC binding when drivers return column data in chunks. Implement incremental reads for SQLGetData and correct byte/unit handling for wide chars and binaries.

Problem

Some ODBC drivers (notably SQL Server) return column data in multiple parts. The previous implementation:

  • mixed bytes and UTF‑16 code unit counts when calling SQLGetData
  • always requested wide-char and misinterpreted returned length
  • did not loop to read remaining data
    This led to garbage bytes inside Dart strings.

Fix

  • Loop-call SQLGetData until the driver signals completion (handle partial reads).
  • Pass buffer sizes in bytes to SQLGetData.
  • For binary columns use SQL_C_BINARY and read into a Uint8List.
  • For wide-char (UTF-16) interpret returned length as bytes and convert by dividing by sizeof(Uint16).
  • Properly handle SQL_NULL_DATA, avoid double-free and fix memory handling.

Files changed

  • lib/src/dart_odbc_base.dart — incremental-read logic, byte/UTF‑16 handling, memory fixes.
  • test/incremental_sqlgetdata_test.dart — integration test that reads a long NVARCHAR and asserts no long runs of non-printable characters.

Test (local)

  1. Put credentials in example/.env
  2. From repo root:
dart pub get
dart test test/incremental_sqlgetdata_test.dart -r expanded

@ccisnedev ccisnedev closed this Oct 21, 2025
@ccisnedev ccisnedev reopened this Oct 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Corrupt characters when reading NVARCHAR from SQL Server (SELECT @@VERSION)

1 participant